import {ref} from 'vue'
import {storeToRefs} from "pinia";
import {useUserStore} from "@/stores";

const userStore = useUserStore();
const {userInformation} = storeToRefs(userStore);

export function useUpload() {
    const imgUrl = ref("");
    const imgUrlCopy = ref("");

    const handleUploadImg = (file: File) => {
        const blob = new Blob([file]);
        imgUrlCopy.value = URL.createObjectURL(blob);
    };
    const view = ref("");

    // 成功回调
    const handleUploadSuccess = (res: any) => {
        const {code, data, msg} = res;
        if (code === 200) {
            imgUrl.value = imgUrlCopy.value;
            view.value = data;
            ElMessage.success("上传成功");
        } else {
            ElMessage.warning(msg);
        }
    };

    // 文件上传失败时的钩子
    const handleUploadError = (err: any) => {
        ElMessage.error("网络错误，请稍后再试");
        return false
    };
    return {
        imgUrl,
        view,
        handleUploadImg,
        handleUploadSuccess,
        handleUploadError,
    }
}